package xyz.icxl.android.architecture.webview

import android.Manifest
import android.util.Log
import android.webkit.*
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.PermissionUtils.SimpleCallback
import com.blankj.utilcode.util.ThreadUtils
import com.blankj.utilcode.util.ToastUtils
import xyz.icxl.android.architecture.dialog.AlertDialog
import xyz.icxl.android.architecture.dialog.ConfirmDialog

/**
 * 文件描述：简单的[MyWebChromeClient]
 *
 * 文件创建人：cxl
 *
 * 创建时间：2022 年 04月 11 日
 *
 * 文件版本：v1.0
 *
 * 版本描述：创建文件
 */
open class MyWebChromeClient(private val fragmentManager: FragmentManager,private val activity: FragmentActivity) : WebChromeClient() {

    /**
     * 重写Alert弹窗
     */
    override fun onJsAlert(
        view: WebView?,
        url: String?,
        message: String?,
        result: JsResult?
    ): Boolean {
        if (!message.isNullOrEmpty() && !fragmentManager.isDestroyed) {
            AlertDialog.show(fragmentManager, message) {
                result?.confirm()
            }
            return true
        }
        return super.onJsAlert(view, url, message, result)
    }

    /**
     * 重写Confirm弹窗
     */
    override fun onJsConfirm(
        view: WebView?,
        url: String?,
        message: String?,
        result: JsResult?
    ): Boolean {
        if (!message.isNullOrEmpty() && result != null && !fragmentManager.isDestroyed) {
            ConfirmDialog.show(
                fragmentManager,
                message,
                object : ConfirmDialog.OnClickButtonListener {
                    override fun onClickRight() {
                        result.confirm()
                    }

                    override fun onClickLeft() {
                        result.cancel()
                    }
                })
            return true
        }
        return super.onJsConfirm(view, url, message, result)
    }

    override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
        if (AppUtils.isAppDebug() && consoleMessage!=null){
            Log.d("HTML-LOG",consoleMessage.message()?:"")
            return true
        }
        return super.onConsoleMessage(consoleMessage)
    }

    /**
     * 加载进度修改回调
     */
    private var _onProgressChange: ((Int) -> Unit)? = null
    fun onProgressChange(action: (Int) -> Unit) {
        _onProgressChange = action
    }

    override fun onProgressChanged(view: WebView?, newProgress: Int) {
        _onProgressChange?.invoke(newProgress)
        super.onProgressChanged(view, newProgress)
    }

    override fun onGeolocationPermissionsShowPrompt(
        origin: String?,
        callback: GeolocationPermissions.Callback?
    ) {
        if(PermissionUtils.isGranted(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION)){
            callback?.invoke(origin,true,false)
        }else{
            activity.runOnUiThread {
                PermissionUtils.permission(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION).callback(object:SimpleCallback{
                    override fun onGranted() {
                        callback?.invoke(origin,true,false)
                    }

                    override fun onDenied() {
                        ToastUtils.showShort("Location permission  not obtained")
                        callback?.invoke(origin,false,false)
                    }
                }).request()
            }
        }
    }
}